package com.apes.scm.demo.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.demo.SampleRequest;
import com.apes.framework.api.SpringManager;
import com.apes.oap.annotation.IgnoreSignType;
import com.apes.oap.annotation.NeedInSessionType;
import com.apes.oap.annotation.ServiceMethod;
import com.apes.oap.annotation.ServiceMethodBean;
import com.apes.oap.response.OapResponse;
import com.apes.scm.demo.model.Order;
import com.apes.scm.demo.model.TestM;
import com.apes.scm.demo.repository.OrderRepository;
import com.apes.scm.demo.repository.TestMRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 功能：订单服务
 *
 * @author xul
 * @create 2017-11-17 15:04
 */
@ServiceMethodBean(version = "1.0", needInSession = NeedInSessionType.NO)
public class OrderService extends DomainService {


    /**
     * 功能：订单保存
     */
//    @Transactional()
    @ServiceMethod(method = "order.save", ignoreSign = IgnoreSignType.YES)
    public OapResponse save(SampleRequest request) {
//        OrderRepository repository = SpringManager.getBean("orderRepository");
//        Order order = new Order();
//        order = repository.save(order);
//        return new OapResponse().setBody(order);
        return null;
    }

    /**
     * 功能：订单更改
     */
    @Transactional()
    @ServiceMethod(method = "order.update", ignoreSign = IgnoreSignType.YES)
    public OapResponse update(SampleRequest request) {
        OrderRepository repository = SpringManager.getBean(OrderRepository.class);
        Order order = repository.findOne(469);
        order.setUserName("orderName");
        repository.save(order);
        return new OapResponse().setBody(order);
    }

    /**
     * 功能：订单删除
     */
    @Transactional()
    @ServiceMethod(method = "order.delete", ignoreSign = IgnoreSignType.YES)
    public OapResponse delete(SampleRequest request) {
        OrderRepository repository = SpringManager.getBean(OrderRepository.class);
        repository.delete(468);
        return new OapResponse().setBody("OK");
    }

    /**
     * 功能：删除所有订单
     */
    @Transactional()
    @ServiceMethod(method = "order.deleteAll", ignoreSign = IgnoreSignType.YES)
    public OapResponse deleteAll(SampleRequest request) {
        OrderRepository repository = SpringManager.getBean(OrderRepository.class);
        repository.deleteAll();
        return new OapResponse().setBody("OK");
    }

    @Autowired
    TestMRepository testMRepository;

    public TestM saveTestM(SimpleRequest request) {
        TestM testM = request.getO(TestM.class);
        return testMRepository.saveAndFlush(testM);
    }
}   